use std::error::Error;
use std::fs::File;
use std::io::Read;

pub fn main() {
    // let f = File::open("hello.txt");
    // let r = match f {
    //     Ok(file) => file,
    //     Err(error) => {
    //         println!("error: {:?}", error);
    //         return;
    //     }
    // };
    // println!("===================");
    // let f = File::open("hello.txt").unwrap();
    // let f = File::open("hello.txt").expect("错误信息");
    println!("===================");
    let result = readUsernameFromFile();
    match result {
        Ok(s) => println!("{}", s),
        Err(error) => println!("文件读取失败")
    }


    match function() {
        Ok(value) => println!("{}", value),
        Err(error) => println!("{}", error),
    }
}

fn readUsernameFromFile() -> Result<String, Box<dyn Error>> {
    let mut f = File::open("hello.txt")?;
    let mut s = String::new();
    f.read_to_string(&mut s)?;
    return Ok(s);
}

fn function() -> Result<i32, Box<dyn Error>> {
    let list = [1, 2];
    let value = *list.get(3).ok_or("发生错误".to_owned())?;
    Ok(value)
}
